package org.richfaces;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.faces.context.FacesContext;

public class Bean {

    private String selectedImage = "dot";
    
    public String getSelectedImage() {
        return selectedImage;
    }
    
    public void setSelectedImage(String selectedImage) {
        this.selectedImage = selectedImage;
    }

    public Object getSelectedImageData() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        InputStream is = facesContext.getExternalContext().getResourceAsStream(selectedImage + ".png");
        try {
            int read;
            byte[] bs = new byte[1024];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((read = is.read(bs)) > 0) {
                baos.write(bs, 0, read);
            }

            return new Data(baos.toByteArray());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                //ignore
            }
        }
        
        return null;
    }
}
